package com.xtkj.superclub_friendscircle.rabbitmq;

import com.xtkj.commons.JsonParse;
import com.xtkj.superclub_entity.entity.friendsCircle.UserLike;
import com.xtkj.superclub_entity.entity.user.User;
import com.xtkj.superclub_friendscircle.client.IUserInfoClient;
import com.xtkj.superclub_friendscircle.test.HttpClientResult;
import com.xtkj.superclub_friendscircle.test.HttpClientUtils;
import io.goeasy.GoEasy;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 项目名称：superclub
 * 类 名 称：HelloReceive
 * 类 描 述：TODO
 * 创建时间：2020/1/6 0:01
 * 创 建 人：xiezhiyan
 */
@Component
@RabbitListener(queues="likeQuery")    //监听器监听指定的Queue
public class LikeReceive {


    @Autowired
    private IUserInfoClient userInfoClient;
    public LikeReceive() {
        System.out.println("加载进来了.............");
    }
    @Autowired
    private RestTemplate restTemplate;



    @RabbitHandler
    public void processC(String str) throws Exception {
        String[] strings = str.split("-");
        String  userLikestr=  strings[0];
        String  token=  strings[1];
        UserLike userLike = JsonParse.string2Obj(userLikestr, UserLike.class);
        //获取该点赞用户的信息，查询点赞人的信息
        Map<String,String> headers = new HashMap<> ();
        headers.put("token",token );
        Map<String,String> params = new HashMap<> ();
        params.put("likeUserId",userLike.getLikesUserId());
        //请求user服务，获取user对象
        String url="http://SUPERCLUB-USER/user/getUserById";
        HttpHeaders requestHeaders = new HttpHeaders();
       //获取token
        requestHeaders.add("token", "a");
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class, params);
        String httpClientResult = response.getBody();
        //HttpClientResult httpClientResult = HttpClientUtils.doGet("http://127.0.0.1:30000/user/getUserById", headers, params);
        User user = JsonParse.string2Obj(httpClientResult, User.class);
        //推送
        GoEasy goEasy = new GoEasy( "https://rest-hangzhou.goeasy.io","BC-33d23be308114797a625a3d1a9a0f4dd");
        //判断点赞和取消点赞
      if(1==userLike.getStatus()){
          goEasy.publish("my_channel", user.getNickName()+":给你点赞了!");
      }
      if(0==userLike.getStatus()){
            goEasy.publish("my_channel", user.getNickName()+":取消给你点赞了!");
      }
        System.out.println("Receive:"+str);
    }
}